<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${empty dict.id ? '发布' : '编辑'}系统字典数据</title>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
</head>
<body>
<!--头部-->
<%@include file="../../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/system/dict/save.do">
                    <input type="hidden" name="id" value="${dict.id}"/>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">字典类型<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <c:choose>
                                    <c:when test="${empty dict.id}"><!-- 新增 -->
                                        <c:if test="${not empty myDict.type}"> <!-- 类型里面新增 -->
                                            <input type="text" name="type" lay-verify="type" autocomplete="off"
                                                   class="layui-input" value="${myDict.type}" readonly/>
                                        </c:if>
                                        <c:if test="${empty myDict.type}"> <!-- 类型外面增加 -->
                                            <input type="text" name="type" lay-verify="type" autocomplete="off"
                                                   class="layui-input"/>
                                        </c:if>
                                    </c:when>
                                    <c:when test="${not empty dict.id}">
                                        <input type="text" name="type" lay-verify="type" autocomplete="off"
                                               class="layui-input" value="${dict.type}"/>
                                    </c:when>
                                </c:choose>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">字典描述<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <c:choose>
                                    <c:when test="${empty dict.id}"><!-- 新增 -->
                                        <c:if test="${not empty myDict.type}"> <!-- 类型里面新增 -->
                                            <input type="text" name="description" lay-verify="description"
                                                   autocomplete="off" class="layui-input" value="${myDict.description}"
                                                   readonly/>
                                        </c:if>
                                        <c:if test="${empty myDict.type}"> <!-- 类型外面增加 -->
                                            <input type="text" name="description" lay-verify="description"
                                                   autocomplete="off" class="layui-input"/>
                                        </c:if>
                                    </c:when>
                                    <c:when test="${not empty dict.id}">
                                        <input type="text" name="description" lay-verify="description"
                                               autocomplete="off" class="layui-input" value="${dict.description}"/>
                                    </c:when>
                                </c:choose>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">字典标签<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="label" lay-verify="label" autocomplete="off"
                                       class="layui-input" value="${dict.label}"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">字典键值<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="value" lay-verify="value" autocomplete="off"
                                       class="layui-input" value="${dict.value}"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">排序<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="sort" lay-verify="sort" autocomplete="off" class="layui-input"
                                       value="${dict.sort}"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit lay-filter="*">立即提交</a>
                            <c:if test="${empty myDict.type}">
                                <a href="${ctx}/system/dict/typeList.do" class="layui-btn layui-btn-primary">取消</a>
                            </c:if>
                            <c:if test="${not empty myDict.type}">
                                <a href="${ctx}/system/dict/dictList.do?type=${myDict.type}"
                                   class="layui-btn layui-btn-primary">取消</a>
                            </c:if>
                        </div>
                    </div>
                </form>
            </div>
        </div><!--底部-->
        <%@include file="../../include/footer.jsp" %>
    </section>
</div>

<script>
    $(function () {
        layui.use(['form', 'laydate'], function () {
            var form = layui.form;

            //自定义验证规则
            form.verify({
                type: function (value) {
                    if (value == "") {
                        return '请填写字典类型';
                    }
                },
                description: function (value) {
                    if (value == "") {
                        return '请填写字典描述';
                    }
                },
                label: function (value) {
                    if (value == "") {
                        return '请填写字典标签';
                    }
                },
                value: function (value) {
                    if (value == "") {
                        return "请填写字典键值";
                    } else {
                        var isRepeat = ajaxSubmit({
                            configId: '${sysConfig.id}',
                            code: value
                        }, '${ctx}/system/config/checkCodeUnique.do');
                        if (isRepeat == false) {
                            return '字典键值已存在';
                        }
                    }
                },
                sort: function (value) {
                    if (!util.checkNumber(value)) {
                        return "请输入正确的数字";
                    }
                }
            });

            //监听提交
            form.on('submit', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    $.post($('#myForm').attr('action'), $('#myForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess("提交成功", function () {
                                if (${empty myDict.type}) {
                                    location.href = "${ctx}/system/dict/typeList.do";
                                } else {
                                    location.href = "${ctx}/system/dict/dictList.do?type=${myDict.type}";
                                }
                            });
                        } else {
                            util.layerMsgError(res.description)
                        }
                    })
                });
                return false;
            });
        });
    });

    function ajaxSubmit(data, url) {
        var isRepeat = false;
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: data,
            dataType: 'json',
            success: function (result) {
                isRepeat = result;
            },
            url: url
        });
        return isRepeat;
    }


</script>
<script type="text/javascript">
    txz.initHeader({
        nav: [{
            name: '字典管理',
            href: '${ctx}/system/dict/typeList.do'
        },
            <c:if test="${empty dict.id}">
                <c:if test="${not empty myDict.type}">
                    {name: '${myDict.type}'},
                </c:if>
            </c:if>
            <c:if test="${not empty dict.id}">
                {name: '${dict.type}'},
            </c:if>
        {
            name: '${empty dict.id ? '发布' : '编辑'}字典数据',
            curr: true
        }],
        btns: [{
            name: '返回',
            href: 'javascript:history.back();',
            icon: 'back'
        }]
    });
</script>
</body>
</html>